package com.wang.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.auth.service.SysLoginLogService;
import com.wang.common.result.Result;
import com.wang.model.system.SysLoginLog;
import com.wang.model.system.dto.SysUserDto;
import com.wang.model.system.vo.SysLoginLogVo;
import com.wang.model.system.vo.SysUserQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统访问记录 前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-04-17
 */
@RestController
@RequestMapping("/admin/system/sysLoginLog")
public class SysLoginLogController {

    @Autowired
    private SysLoginLogService loginLogService;
    @ApiOperation("用户条件分页查询")
    @GetMapping("/{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    public Result index(@PathVariable Long page, @PathVariable Long limit, SysLoginLogVo sysLoginLogVo){
        Page<SysLoginLog> sysLoginLogPage = new Page<>(page,limit);
        LambdaQueryWrapper<SysLoginLog>lambdaQueryWrapper  = new LambdaQueryWrapper<>();
        String keyword = sysLoginLogVo.getKeyword();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(keyword), SysLoginLog::getUsername, keyword);
        lambdaQueryWrapper.ge(sysLoginLogVo.getCreateTimeBegin() != null,SysLoginLog::getCreateTime,sysLoginLogVo.getCreateTimeBegin());
        lambdaQueryWrapper.le(sysLoginLogVo.getCreateTimeBegin() != null,SysLoginLog::getCreateTime,sysLoginLogVo.getCreateTimeEnd());
        lambdaQueryWrapper.orderByDesc(SysLoginLog::getAccessTime);
        return Result.success(loginLogService.page(sysLoginLogPage,lambdaQueryWrapper));
    }
}
